package com.lorenzogao.latte_core.app

import java.lang.RuntimeException
import java.util.*

/**
 *  create by LorenzoGao
 *  email: 2508719070@qq.com
 *  TIME: 2020-01-06 19:20
 *  description:
 */

class Configurator {


    val LATTE_CONFIGS by lazy {
        WeakHashMap<String, Any>()
    }


    init {
        LATTE_CONFIGS[ConfigType.CONFIT_READY.name] = false

    }

    companion object {

        val INSTANCE by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
            Configurator()
        }



    }

    fun configure() {
        LATTE_CONFIGS[ConfigType.CONFIT_READY.name] = true
    }


    fun withApiHost(host: String): Configurator {
        LATTE_CONFIGS[ConfigType.API_HOST.name] = host
        return this
    }


    private fun checkConfiguration() {
        val isRead = LATTE_CONFIGS[ConfigType.CONFIT_READY.name] as Boolean

        if (!isRead) {
            throw RuntimeException("Configuration is not ready,call configure")
        }

    }


    @SuppressWarnings("unchecked")
    fun <T> getConfiguration(key :Enum<ConfigType>): T {
        checkConfiguration()
        return LATTE_CONFIGS[key.name] as T
    }


}